/* =========================================================================

DESCRIPTION
   cae_app

Copyright (c) 2015 by  USTC iFLYTEK, Co,LTD.  All Rights Reserved.
============================================================================ */

/* =========================================================================

                             REVISION

when            who              why
--------        ---------        -------------------------------------------
2015/12/22     qfgao               Created.
============================================================================ */
#include <stdio.h>
#include <assert.h>
#include <stdlib.h>
#include <string.h>
#include <alsa/asoundlib.h>
#include <pthread.h>
#include <unistd.h>
#include <fcntl.h>
#include<sys/types.h>
#include<sys/stat.h>

#include "iic_test.h"


unsigned int fd_temp=open("./temp.txt",O_RDWR|O_CREAT, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH);

int qu_sum=0;
int re_sum=0;
int en_sum=0;
int s_sum=0;
int s0_sum=0;
int s1_sum=0;
int s2_sum=0;
int s3_sum=0;

bool query_version(int fd)
{
	qu_sum++;
	
	if(fd<0)
	{
		printf("OPEN_DEV_ERR\n");
		char string[100]="time:";
		write(fd_temp,string,5);
		
		char str[2];
		str[0]=(qu_sum/10)+'0';
		str[1]=(qu_sum%10)+'0';
		write(fd_temp,str,2);
		
		char string1[100]=" reason:";
		write(fd_temp,string1,8);
		write(fd_temp,"OPEN_DEV_ERR",12);
		write(fd_temp,"\r\n",2);
		return false;
	}
	
	bool rv;
	unsigned char cmds[]={0x00,0x0f,0x00,0x00};
	int cnt = 0;
	rv = write_i2c_device(fd,0,cmds);
	if(!rv)
	{
		printf("WRITE_I2C_ERR\n");
		char string[100]="time:";
		write(fd_temp,string,5);
		
		char str[2];
		str[0]=(qu_sum/10)+'0';
		str[1]=(qu_sum%10)+'0';
		write(fd_temp,str,2);
		
		char string1[100]=" reason:";
		write(fd_temp,string1,8);
		write(fd_temp,"WRITE_I2C_ERR",13);
		write(fd_temp,"\r\n",2);
		return false;
	}
		
	while(rv && cmds[0] == 0){
		while(!(rv = read_i2c_device(fd,0,cmds)))
		{
			cnt ++;
			if(cnt>100)
			{
				printf("READ_I2C_ERR\n");
				char string[100]="time:";
		        write(fd_temp,string,5);
		
		        char str[2];
		        str[0]=(qu_sum/10)+'0';
		        str[1]=(qu_sum%10)+'0';
		        write(fd_temp,str,2);
		
		        char string1[100]=" reason:";
		        write(fd_temp,string1,8);
		        write(fd_temp,"READ_I2C_ERR",12);
		        write(fd_temp,"\r\n",2);
				return false;
			}
		}
		if(cnt>100) break;
	}
	while(cmds[2] == 0x02 && cmds[3] == 0x00){
	
		unsigned char pbuff1[4]={0,0,0,0};
		unsigned char pbuff2[4]={0,0,0,0};
		rv=0;
		while(!rv)
		{
			rv=read_i2c_device(fd,0x01,pbuff1);
			rv=read_i2c_device(fd,0x02,pbuff2);
			cnt++;
			if(pbuff1[3]==0x00){
				break;
			}
			if(cnt>100){
				printf("get version fail\n");
				return false;
			}
		}
		if(cnt<=100){
			char buf[100];
			int i=(int)pbuff1[0];
			int tmp1 = pbuff2[1];
			int tmp2 = pbuff2[2];
			int tmp3 = pbuff2[3];
			int j= pbuff2[0] | (tmp1<<8) | (tmp2<<16) | (tmp3<<24);
			int version_m = j & 4095;
			int cae_ver = ((j) >> 12) & 8191;
			int channel_num = ((j) >> 25) & 31;
			sprintf(buf,"version is 1.0.%d.%d\ncae_ver is:%d\nchannel_num is:%d\n",i,version_m,cae_ver,channel_num);
			printf("%s",buf);	
		}
		printf("qurry version is ok!!!\n");	
		return rv;
	}
	
	return false;
}

bool read_angle(int fd,int *angleee)
{
	re_sum++;
	bool rv;
	unsigned char cmds[]={0x00,0x10,0x00,0x00};
	int cnt = 0;
	
	if(fd<0)
	{
		printf("OPEN_DEV_ERR\n");
		char string[100]="time:";
		write(fd_temp,string,5);
		
		char str[2];
		str[0]=(re_sum/10)+'0';
		str[1]=(re_sum%10)+'0';
		write(fd_temp,str,2);
		
		char string1[100]=" reason:";
		write(fd_temp,string1,8);
		write(fd_temp,"OPEN_DEV_ERR",12);
		write(fd_temp,"\r\n",2);
		return false;
	}
	
	rv = write_i2c_device(fd,0,cmds);
	if(!rv)
	{
		printf("WRITE_I2C_ERR\n");
		char string[100]="time:";
		write(fd_temp,string,5);
		
		char str[2];
		str[0]=(re_sum/10)+'0';
		str[1]=(re_sum%10)+'0';
		write(fd_temp,str,2);
		
		char string1[100]=" reason:";
		write(fd_temp,string1,8);
		write(fd_temp,"WRITE_I2C_ERR",13);
		write(fd_temp,"\r\n",2);
		return false;
	}
	
	while(rv && cmds[0] == 0){
		while(!(rv = read_i2c_device(fd,0,cmds)))
		{
			cnt ++;
			if(cnt>100)
			{ 
		        printf("READ_I2C_ERR\n");
        		char string[100]="time:";
		        write(fd_temp,string,5);
		
		        char str[2];
		        str[0]=(re_sum/10)+'0';
	        	str[1]=(re_sum%10)+'0';
	        	write(fd_temp,str,2);
		
		        char string1[100]=" reason:";
		        write(fd_temp,string1,8);
		        write(fd_temp,"READ_I2C_ERR",12);
		        write(fd_temp,"\r\n",2);
			return false;
			}
		}
		if(cnt>100) break;
	}
	
	while(cmds[0] == 0x01 && cmds[1] == 0x00){
		//printf("rv:%x\tcmds[0]:%x\tcmds[1]:%x\tcmds[2]:%x\tcmds[3]:%x\t\n",rv,cmds[0],cmds[1],cmds[2],cmds[3]);	
		unsigned char apbuff1[4]={0,0,0,0};
		rv=read_i2c_device(fd,0x01,apbuff1);
		char buf[100];
		int temp = apbuff1[1];
		int i= apbuff1[0] | (temp<<8); 
        *angleee=i;
		sprintf(buf,"angle: %d \n",i);
		//printf("%s",buf);	
		//printf("read_angle is ok!!!\n");
        return rv;
    }
	return false;
}

bool enter_reset(int fd)
{
	en_sum++;
	bool rv;
	unsigned char cmds[]={0x00,0x11,0x00,0x00};
	int cnt = 0;
	
	if(fd<0)
	{
		printf("OPEN_DEV_ERR\n");
		char string[100]="time:";
		write(fd_temp,string,5);
		
		char str[2];
		str[0]=(en_sum/10)+'0';
		str[1]=(en_sum%10)+'0';
		write(fd_temp,str,2);
		
		char string1[100]=" reason:";
		write(fd_temp,string1,8);
		write(fd_temp,"OPEN_DEV_ERR",12);
		write(fd_temp,"\r\n",2);
		return false;
	}
	
	rv = write_i2c_device(fd,0,cmds);
	if(!rv)
	{
		printf("WRITE_I2C_ERR\n");
		char string[100]="time:";
		write(fd_temp,string,5);
		
		char str[2];
		str[0]=(en_sum/10)+'0';
		str[1]=(en_sum%10)+'0';
		write(fd_temp,str,2);
		
		char string1[100]=" reason:";
		write(fd_temp,string1,8);
		write(fd_temp,"WRITE_I2C_ERR",13);
		write(fd_temp,"\r\n",2);
		return false;
	}
	
	while(rv && cmds[0] == 0){
		while(!(rv = read_i2c_device(fd,0,cmds)))
		{
			cnt ++;
			if(cnt>100) 
			{
				printf("READ_I2C_ERR\n");
				char string[100]="time:";
		        write(fd_temp,string,5);
		
		        char str[2];
		        str[0]=(en_sum/10)+'0';
 	        	str[1]=(en_sum%10)+'0';
	        	write(fd_temp,str,2);
		
		        char string1[100]=" reason:";
		        write(fd_temp,string1,8);
		        write(fd_temp,"READ_I2C_ERR",12);
		        write(fd_temp,"\r\n",2);
				return false;
			}
		}
		if(cnt>100) break;
	}
	
	//printf("rv:%x\tcmds[0]:%d\tcmds[1]:%d\tcmds[2]:%d\tcmds[3]:%d\t\n",rv,cmds[0],cmds[1],cmds[2],cmds[3]);
	while(cmds[2] == 0x00 && cmds[3] == 0x00){
		printf("reset is ok!!!\n");
        return rv;
        }
	return false;
}

bool set_beam0( int fd)
{
	s0_sum++;
	bool rv;
	unsigned char cmds[]={0x00,0x12,0x00,0x00};
	int cnt = 0;
	
	if(fd<0)
	{
		printf("OPEN_DEV_ERR\n");
		char string[100]="time:";
		write(fd_temp,string,5);
		
		char str[2];
		str[0]=(s0_sum/10)+'0';
		str[1]=(s0_sum%10)+'0';
		write(fd_temp,str,2);
		
		char string1[100]=" reason:";
		write(fd_temp,string1,8);
		write(fd_temp,"OPEN_DEV_ERR",12);
		write(fd_temp,"\r\n",2);
		return false;
	}
	
	rv = write_i2c_device(fd,0,cmds);
	if(!rv)
	{
		printf("WRITE_I2C_ERR\n");
		char string[100]="time:";
		write(fd_temp,string,5);
		
		char str[2];
		str[0]=(s0_sum/10)+'0';
		str[1]=(s0_sum%10)+'0';
		write(fd_temp,str,2);
		
		char string1[100]=" reason:";
		write(fd_temp,string1,8);
		write(fd_temp,"WRITE_I2C_ERR",13);
		write(fd_temp,"\r\n",2);
		return false;
	}
	
	
	while(rv && cmds[0] == 0){
		while(!(rv = read_i2c_device(fd,0,cmds)))
		{
			cnt ++;
			if(cnt>100) 
			{
				printf("READ_I2C_ERR\n");
				char string[100]="time:";
		        write(fd_temp,string,5);
		
		        char str[2];
		        str[0]=(s0_sum/10)+'0';
	        	str[1]=(s0_sum%10)+'0';
        		write(fd_temp,str,2);
		
		        char string1[100]=" reason:";
		        write(fd_temp,string1,8);
		        write(fd_temp,"READ_I2C_ERR",12);
		        write(fd_temp,"\r\n",2);
				return false;
			}
		}
		if(cnt>100) break;
	}
	
	while(cmds[2] == 0x03 && cmds[3] == 0x00){
		printf("set beam0 is ok!!!\n");
        return rv;
        }
	return false;
}

bool set_beam1(int fd)
{
	s1_sum++;
	bool rv;
	unsigned char cmds[]={0x00,0x12,0x01,0x00};
	int cnt = 0;
	
	if(fd<0)
	{
		printf("OPEN_DEV_ERR\n");
		char string[100]="time:";
		write(fd_temp,string,5);
		
		char str[2];
		str[0]=(s1_sum/10)+'0';
		str[1]=(s1_sum%10)+'0';
		write(fd_temp,str,2);
		
		char string1[100]=" reason:";
		write(fd_temp,string1,8);
		write(fd_temp,"OPEN_DEV_ERR",12);
		write(fd_temp,"\r\n",2);
		return false;
	}
	
	rv = write_i2c_device(fd,0,cmds);
	if(!rv)
	{
		printf("WRITE_I2C_ERR\n");
		char string[100]="time:";
		write(fd_temp,string,5);
		
		char str[2];
		str[0]=(s1_sum/10)+'0';
		str[1]=(s1_sum%10)+'0';
		write(fd_temp,str,2);
		
		char string1[100]=" reason:";
		write(fd_temp,string1,8);
		write(fd_temp,"WRITE_I2C_ERR",13);
		write(fd_temp,"\r\n",2);
		return false;
	}
	
	while(rv && cmds[0] == 0){
		while(!(rv = read_i2c_device(fd,0,cmds)))
		{
			cnt ++;
			if(cnt>100) 
			{
				printf("READ_I2C_ERR\n");
				char string[100]="time:";
		        write(fd,string,5);
		
		        char str[2];
		        str[0]=(s1_sum/10)+'0';
        		str[1]=(s1_sum%10)+'0';
	        	write(fd_temp,str,2);
		
		        char string1[100]=" reason:";
		        write(fd_temp,string1,8);
		        write(fd_temp,"READ_I2C_ERR",12);
		        write(fd_temp,"\r\n",2);
				return false;
			}
		}
		if(cnt>100) break;
	}
	
	while(cmds[2] == 0x03 && cmds[3] == 0x00){
		printf("set beam1 is ok!!!\n");
        return rv;
        }
	return false;
}

bool set_beam2(int fd)
{
	s2_sum++;
	bool rv;
	unsigned char cmds[]={0x00,0x12,0x02,0x00};
	int cnt = 0;
	
	if(fd<0)
	{
		printf("OPEN_DEV_ERR\n");
		char string[100]="time:";
		write(fd_temp,string,5);
		
		char str[2];
		str[0]=(s2_sum/10)+'0';
		str[1]=(s2_sum%10)+'0';
		write(fd_temp,str,2);
		
		char string1[100]=" reason:";
		write(fd_temp,string1,8);
		write(fd_temp,"OPEN_DEV_ERR",12);
		write(fd_temp,"\r\n",2);
		return false;
	}
	
	rv = write_i2c_device(fd,0,cmds);
	if(!rv)
	{
		printf("WRITE_I2C_ERR\n");
		char string[100]="time:";
		write(fd_temp,string,5);
		
		char str[2];
		str[0]=(s2_sum/10)+'0';
		str[1]=(s2_sum%10)+'0';
		write(fd_temp,str,2);
		
		char string1[100]=" reason:";
		write(fd_temp,string1,8);
		write(fd_temp,"WRITE_I2C_ERR",13);
		write(fd_temp,"\r\n",2);
		return false;
	}
	rv = read_i2c_device(fd,0,cmds);
	
	while(rv && cmds[0] == 0){
		while(!(rv = read_i2c_device(fd,0,cmds)))
		{
			cnt ++;
			if(cnt>100)
			{	
		        printf("READ_I2C_ERR\n");
		        char string[100]="time:";
		        write(fd_temp,string,5);
		
		        char str[2];
		        str[0]=(s2_sum/10)+'0';
		        str[1]=(s2_sum%10)+'0';
	        	write(fd_temp,str,2);
		
		        char string1[100]=" reason:";
		        write(fd_temp,string1,8);
		        write(fd_temp,"READ_I2C_ERR",12);
		        write(fd_temp,"\r\n",2);
		        return false;
			}
		}
		if(cnt>100) break;
	}
	
	while(cmds[2] == 0x03 && cmds[3] == 0x00){
		printf("set beam2 is ok!!!\n");
        return rv;
        }
	return false;
}

bool set_beam3(int fd)
{
	s3_sum++;
	bool rv;
	unsigned char cmds[]={0x00,0x12,0x03,0x00};
	int cnt = 0;
	
	if(fd<0)
	{
		printf("OPEN_DEV_ERR\n");
		char string[100]="time:";
		write(fd_temp,string,5);
		
		char str[2];
		str[0]=(s3_sum/10)+'0';
		str[1]=(s3_sum%10)+'0';
		write(fd_temp,str,2);
		
		char string1[100]=" reason:";
		write(fd_temp,string1,8);
		write(fd_temp,"OPEN_DEV_ERR",12);
		write(fd_temp,"\r\n",2);
		return false;
	}
	
	rv = write_i2c_device(fd,0,cmds);
	if(!rv)
	{
		printf("WRITE_I2C_ERR\n");
		char string[100]="time:";
		write(fd_temp,string,5);
		
		char str[2];
		str[0]=(s3_sum/10)+'0';
		str[1]=(s3_sum%10)+'0';
		write(fd_temp,str,2);
		
		char string1[100]=" reason:";
		write(fd_temp,string1,8);
		write(fd_temp,"WRITE_I2C_ERR",13);
		write(fd_temp,"\r\n",2);
		return false;
	}
	
	while(rv && cmds[0] == 0){
		while(!(rv = read_i2c_device(fd,0,cmds)))
		{
			cnt ++;
			if(cnt>100) {
				printf("READ_I2C_ERR\n");
				char string[100]="time:";
		        write(fd_temp,string,5);
		
		        char str[2];
		        str[0]=(s3_sum/10)+'0';
	        	str[1]=(s3_sum%10)+'0';
	        	write(fd_temp,str,2);
		
		        char string1[100]=" reason:";
		        write(fd_temp,string1,8);
		        write(fd_temp,"READ_I2C_ERR",12);
		        write(fd_temp,"\r\n",2);
				return false;
			}
		}
		if(cnt>100) break;
	}
	
	while(cmds[2] == 0x03 && cmds[3] == 0x00){
		printf("set beam3 is ok!!!\n");
        return rv;
        }
	return false;
}

bool set_beam(int fd)
{
	s_sum++;
	bool rv;
	unsigned char cmds[]={0x00,0x12,0x02,0x00};
	int cnt = 0;
	
	if(fd<0)
	{
        printf("OPEN_DEV_ERR\n");
		char string[100]="time:";
		write(fd_temp,string,5);
		
		char str[2];
		str[0]=(s_sum/10)+'0';
		str[1]=(s_sum%10)+'0';
		write(fd_temp,str,2);
		
		char string1[100]=" reason:";
		write(fd_temp,string1,8);
		write(fd_temp,"OPEN_DEV_ERR",12);
		write(fd_temp,"\r\n",2);
		return false;
	}
	
	rv = write_i2c_device(fd,0,cmds);
	printf("%d\n",rv);
	if(!rv)
	{
		printf("WRITE_I2C_ERR\n");
		char string[100]="time:";
		write(fd_temp,string,5);
		
		char str[2];
		str[0]=(s_sum/10)+'0';
		str[1]=(s_sum%10)+'0';
		write(fd_temp,str,2);
		
		char string1[100]=" reason:";
		write(fd_temp,string1,8 );
		write(fd,"WRITE_I2C_ERR",13);
		write(fd,"\r\n",2);
		return false;
	}
	
	while(rv && cmds[0] == 0){
		while(!(rv = read_i2c_device(fd,0,cmds)))
		{
			cnt ++;
			if(cnt>100) 
			{
				printf("READ_I2C_ERR\n");
				char string[100]="time:";
		        write(fd,string,5);
		
		        char str[2];
		        str[0]=(s_sum/10)+'0';
		        str[1]=(s_sum%10)+'0';
		        write(fd_temp,str,2);
		
		        char string1[100]=" reason:";
		        write(fd_temp,string1,8);
		        write(fd_temp,"READ_I2C_ERR",12);
		        write(fd_temp,"\r\n",2);
				return false;
			}
		}
		if(cnt>100) break;
	}
	
	while(cmds[2] == 0x03 && cmds[3] == 0x00){
		printf("set beam is ok!!!\n");
        return rv;
        }
	return false;
	
	close(fd_temp);
}


